import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Headers, RequestOptions } from '@angular/http';
import { SessionService } from '../services/session.service'; 
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch'
@Injectable()
export class HttpService {

    constructor(private http: Http) {
    }

    public httpPost(object: any, url: string): Observable<any> {
 
     
        let body = JSON.stringify(object);

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('Accept', 'q=0.8;application/json;q=0.9');

        if (typeof (Storage) !== "undefined") {

            let token = localStorage.getItem("dapptoken");
            headers.append('Authorization',"bearer "+ token);
        }

        let options = new RequestOptions({ headers: headers });

        return this.http.post(url, body, options).map((response) => this.parseResponse(response, true))
            .catch((err) => this.handleError(err,true));

    }

 public httpGet( url: string): Observable<any> {
 
     

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        //headers.append('Accept', 'q=0.8;application/json;q=0.9');

        if (typeof (Storage) !== "undefined") {

            let token = localStorage.getItem("dapptoken");
            headers.append('Authorization',"bearer "+ token);
        }

        let options = new RequestOptions({ headers: headers });

        return this.http.get(url, options).map((response) => this.parseResponse(response, true))
            .catch((err) => this.handleError(err,true));

    }

 


    public httpPostWithNoBlock(object: any, url: string): Observable<any> {

        let body = JSON.stringify(object);

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('Accept', 'q=0.8;application/json;q=0.9');

        if (typeof (Storage) !== "undefined") {

            let token = localStorage.getItem("dapptoken");
            // console.log(token)
            // console.log("===");
            headers.append('Authorization', token);
        }

        let options = new RequestOptions({ headers: headers });

        return this.http.post(url, body, options).map((response) =>this.parseResponse(response,true)  
         )
            .catch((err) => this.handleError(err,false));

    }
   



    private handleError(error: any, blocking: Boolean) {
        let body = error.json(); 
        return Observable.throw(body);
    }
      
    private parseResponse(response: Response,  blocking: Boolean) {
       // console.log(response.headers);
        let authorizationToken = response.headers.get("Authorization");
         
        if (authorizationToken != null) {
           
            if (typeof (Storage) !== "undefined") {
                localStorage.setItem("dapptoken", authorizationToken);
            }
        }
 
     
        let body = response.json();

        return body;
    }


}